home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / lfcr.zip / LFCR8.ASM < prev    next >
Assembly Source File  |  1986-09-21  |  7KB  |  272 lines

  1.     title "Replace LineFeed or Carriage Return with CR/LF."
  2.  
  3. filefix    segment
  4.     assume ds:filefix,ss:filefix,cs:filefix,es:filefix
  5.  
  6. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  7. ;  Buffer Data Base.
  8. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  9. filfcb        equ 05ch
  10.  
  11. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  12. ;  Data Base Fix.
  13. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  14. filefix_start:
  15.  
  16.     push cs
  17.     pop ds                ;insure ds same as cs.
  18.     xor si,si
  19.     mov ch,40
  20.     mov bx,offset filfcb
  21. filefix_04:
  22.     mov al,byte ptr es:[bx]        ;copy file control block
  23.     mov byte ptr inputfcb[si],al
  24.     inc bx
  25.     inc si
  26.     dec ch
  27.     jnz filefix_04
  28.  
  29.     push ds
  30.     pop es                ;set es:
  31.     mov si,9
  32. filefix_08:
  33.     mov al,byte ptr inputfcb-1[si]
  34.     mov byte ptr outputfcb-1[si],al
  35.     dec si
  36.     jnz filefix_08
  37.  
  38. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  39. ;  attempt file open.
  40. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  41.     mov ah,15
  42.     mov dx,offset inputfcb
  43.     mov word ptr inputfcb+33,0    ;set record address to zero.
  44.     int 21h                ;open file
  45.     cmp al,-1            ;file available ?
  46.     jz dos_exit_cancel        ;no -->
  47.  
  48.     mov ah,22
  49.     mov dx,offset outputfcb
  50.     mov word ptr outputfcb+33,0    ;set record address to zero.
  51.     int 21h                ;create output file
  52.     cmp al,-1            ;file available ?
  53.     jz dos_exit_cancel        ;no -->
  54.  
  55.     mov byte ptr inputbuffer-1,-1
  56.     mov word ptr outputbuffer-2,0
  57.     jmp short filefix_20
  58.  
  59. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  60. ;  Exit
  61. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  62. dos_exit_cancel:
  63.     mov ax,offset 4c00h
  64.     int 21h
  65.  
  66. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  67. ;  read characters...
  68. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  69. filefix_20:
  70.     call filefix_getcharacter
  71.     jc filefix_40            ;if all done -->
  72.     cmp al,"Q"-40h
  73.     jz filefix_20            ;ignore ctl-Q -->
  74.     cmp al,"S"-40h
  75.     jz filefix_20            ;ignore ctl-S -->
  76.     cmp al,"M"-40h
  77.     jz filefix_24            ;ignore ctl-m -->
  78.     cmp al,"J"-40h
  79.     jz filefix_24            ;ignore ctl-j -->
  80.  
  81.     call filefix_storecharacter
  82.     jmp filefix_20
  83.  
  84. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  85. ;  fix end of line.
  86. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  87. filefix_24:
  88.     call filefix_getcharacter
  89.     cmp al,"Q"-40h
  90.     jz filefix_24            ;ignore ctl-Q -->
  91.     cmp al,"S"-40h
  92.     jz filefix_24            ;ignore ctl-S -->
  93.     cmp al,"M"-40h
  94.     jz filefix_24            ;ignore ctl-m -->
  95.     cmp al,"J"-40h
  96.     jz filefix_24            ;ignore ctl-j -->
  97.  
  98.     push ax
  99.     mov al,"M"-40h
  100.     call filefix_storecharacter
  101.     mov al,"J"-40h
  102.     call filefix_storecharacter
  103.     pop ax
  104.     call filefix_storecharacter
  105.     jmp filefix_20
  106.  
  107. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  108. ;  all done.
  109. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  110. filefix_40:
  111.     mov al,"M"-40h
  112.     call filefix_storecharacter
  113.     mov al,"J"-40h
  114.     call filefix_storecharacter
  115.     mov al,"Z"-40h
  116.     call filefix_storecharacter
  117.  
  118.     call filefix_writebuffer
  119.  
  120.     mov ah,16
  121.     mov dx,offset outputfcb
  122.     int 21h                ;close write file.
  123.  
  124.     mov ah,19
  125.     mov dx,offset inputfcb
  126.     int 21h                ;delete old (input) file.
  127.  
  128.     mov si,offset 11
  129. filefix_44:
  130.     mov al,byte ptr inputfcb[si]
  131.     mov byte ptr outputfcb+16[si],al
  132.     dec si
  133.     jnz filefix_44
  134.  
  135.     mov ah,23
  136.     mov dx,offset outputfcb
  137.     int 21h
  138.     jmp dos_exit_cancel
  139.  
  140. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  141. ;  get character from InputBuffer.
  142. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  143. filefix_getcharacter:
  144.     mov al,byte ptr inputbuffer-1
  145.     cmp al,-1            ;buffer empty (non-init) ?
  146.     jnz filefix_get_04        ;no, go get character -->
  147.     call filefix_readrecord
  148.  
  149. filefix_get_04:
  150.     xor bx,bx
  151.     mov bl,byte ptr inputbuffer-1
  152.     mov al,byte ptr inputbuffer[bx]
  153.     and al,127
  154.     cmp al,"Z"-40h            ;control Z ?
  155.     stc                ;yes, set flag
  156.     jz filefix_get_ret        ;don't do another read -->
  157.  
  158.     inc byte ptr inputbuffer-1
  159.     cmp bl,127
  160.     jnz filefix_get_08        ;exit -->
  161.     push ax
  162.     call filefix_readrecord
  163.     pop ax
  164.  
  165. filefix_get_08:
  166.     or al,al
  167.  
  168. filefix_get_ret:
  169.     ret
  170.  
  171. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  172. ;  Read File
  173. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  174. filefix_readrecord:
  175.     mov ah,26
  176.     mov dx,offset inputbuffer
  177.     int 21h
  178.     mov ah,20
  179.     mov dx,offset inputfcb
  180.     int 21h
  181.     or al,al
  182.     jz filefix_readrecord_08
  183.     dec al                ;error code 1 ?
  184.     jz filefix_readrecord_06    ;yes, entire record at end of file -->
  185.  
  186.     mov bx,offset 128
  187.  
  188. filefix_readrecord_04:
  189.     or bx,bx
  190.     jz filefix_readrecord_08
  191.     dec bx
  192.     mov al,byte ptr inputbuffer-1[bx]
  193.     or al,al
  194.     jnz filefix_readrecord_08
  195.     mov byte ptr inputbuffer-1[bx],"Z"-40h
  196.     jmp filefix_readrecord_04
  197.  
  198. filefix_readrecord_06:
  199.     mov byte ptr inputbuffer,"Z"-40h
  200.  
  201. filefix_readrecord_08:
  202.     inc word ptr inputfcb+33
  203.     mov byte ptr inputbuffer-1,0
  204.     ret
  205.  
  206. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  207. ;  Store Character.
  208. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  209. filefix_storecharacter:
  210.     push ax
  211.     mov ah,"Z"-40h            ;automatically patch end of file
  212.     mov bx,word ptr outputbuffer-2
  213.     mov word ptr outputbuffer[bx],ax;store character(s)
  214.     inc word ptr outputbuffer-2
  215.  
  216.     cmp bx,offset 128*128-1        ;sixteen K?
  217.     jnz filefix_storecharacter_08
  218.     call filefix_writebuffer
  219.  
  220. filefix_storecharacter_08:
  221.     pop ax
  222.     ret
  223.  
  224. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  225. ;  output buffer (may be partially filled).
  226. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  227. filefix_writebuffer:
  228.     mov ax,word ptr outputbuffer-2
  229.     or ax,ax            ;anything to write ?
  230.     jz filefix_writebuffer_return    ;no -->
  231.  
  232.     mov dx,offset outputbuffer
  233.  
  234. filefix_writebuffer_04:
  235.     push dx
  236.     mov ah,26
  237.     int 21h
  238.  
  239.     mov ah,34
  240.     mov dx,offset outputfcb
  241.     int 21h
  242.     inc word ptr outputfcb+33
  243.     pop dx
  244.     add dx,offset 128
  245.     sub word ptr outputbuffer-2,128
  246.     jz filefix_writebuffer_return
  247.     cmp word ptr outputbuffer-2,128
  248.     jnc filefix_writebuffer_04
  249.  
  250.     mov word ptr outputbuffer-2,128
  251.     jmp filefix_writebuffer_04
  252.  
  253. filefix_writebuffer_return:
  254.     mov word ptr outputbuffer-2,0
  255.     ret
  256.  
  257. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  258. ;  Buffer Data Base.
  259. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  260. inputfcb:    db 40 dup(0)
  261. outputfcb:    db 0,"________$$$",30 dup(0)
  262.  
  263.         db -1
  264. inputbuffer:    db 128 dup(0)
  265.  
  266.         dw 0
  267. outputbuffer:    db 128 dup(0)        ;actually, goes for many K...
  268.  
  269. filefix    ends
  270.     end filefix_start
  271.  
  272.